<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of m_login
 *
 * @author Trung
 */
class M_user extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function getUser($maso, $matkhau) {
        $this->db->where('MaSo', $maso);
        $this->db->where('Ma_SV', $maso);
        $this->db->where('MatKhau', $matkhau);
        $this->db->from('tbl_sinh_vien');
        $this->db->from('tbl_user');
        $this->db->join('tbl_diadiemhoc', 'Ma_DiaDiemHoc=FK_Ma_DiaDiemHoc_SV');
        $query1 = $this->db->get();







        $this->db->where('MaSo', $maso);
        $this->db->where('Ma_CB', $maso);
        $this->db->where('MatKhau', $matkhau);
        $this->db->from('tbl_canbo');
        $this->db->from('tbl_user');
        $this->db->join('tbl_diadiemhoc', 'Ma_DiaDiemHoc=FK_Ma_DiaDiemCongTac_CB');
        $query2 = $this->db->get();
        $data = FALSE;
        if ($query1->num_rows() > 0) {
            $this->db->where('FK_Ma_SV', $maso);
            $this->db->order_by('KyHoc', "desc");
            $query11 = $this->db->get('tbl_sv_hocphi');
            $data = $query1->row_array();
            $data['HocPhi'] = 1;
            foreach ($query11->result_array() as $row) {
                if ($row['Trangthai'] == 0) {
                    $data['HocPhi'] = 0;
                }
            }
        } else {
            if ($query2->num_rows() > 0) {
                $data = $query2->row_array();
            }
        }
        
        return $data;
    }

    public function getUserSinhVien($maso, $matkhau) {
        $this->db->where('MaSo', $maso);
        $this->db->where('Ma_SV', $maso);
        $this->db->where('MatKhau', $matkhau);
        $this->db->from('tbl_sinh_vien');
        $this->db->from('tbl_user');
        $this->db->join('tbl_diadiemhoc', 'Ma_DiaDiemHoc=FK_Ma_DiaDiemHoc_SV');
        $query = $this->db->get();

        $data = NULL;
        if ($query->num_rows() == 0) {
            $data = NULL;
        } else {


            $data = $query->row_array();
        }
        return $data;
    }

    public function getUserCanBo($maso, $matkhau) {
        $this->db->where('MaSo', $maso);
        $this->db->where('Ma_CB', $maso);
        $this->db->where('MatKhau', $matkhau);
        $this->db->from('tbl_canbo');
        $this->db->from('tbl_user');
        $this->db->join('tbl_diadiemhoc', 'Ma_DiaDiemHoc=FK_Ma_DiaDiemCongTac_CB');
        $query = $this->db->get();
        $data = NULL;
        if ($query->num_rows() == 0) {
            $data = NULL;
        } else {


            $data = $query->row_array();
        }
        return $data;
    }

    public function getUserEmail($tendangnhap) {
        $this->db->where('MaSo', $tendangnhap);


        $this->db->from('tbl_user');

        $query = $this->db->get();
        $data = $query->row_array();
        return $data;
    }

    public function kiemTraUser(String $TenDangNhap) {
        if (strlen($TenDangNhap) == 9) {
            return 1;
        } else {
            return 0;
        }
    }

    public function setPassword($tendangnhap, $matkhau) {
        
    }

}
